package com.gxa.service.staffservice.impl;

import com.gxa.dao.staffdao.StaffMapper;
import com.gxa.pojo.staff.UserStaff;
import com.gxa.service.staffservice.StaffService;
import com.gxa.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.Map;

public class StaffServiceImpl implements StaffService {


    @Override
    public List<UserStaff> findAll() {
        //获取工厂和sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        List<UserStaff> userStaffs = mapper.selectAll();
        return  userStaffs;
    }

    @Override
    public Boolean add(UserStaff userStaff) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        Boolean aBoolean = mapper.addStaff(userStaff);
        return aBoolean;
    }

    @Override
    public UserStaff findById(int id) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        UserStaff userStaff = mapper.selectById(id);
        return userStaff;
    }

    @Override
    public Boolean update(UserStaff userStaff) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        Boolean aBoolean = mapper.updateUser(userStaff);
        return  aBoolean;
    }

    @Override
    public Boolean clockStaff(int staff_id) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        Boolean aBoolean = mapper.clockUser(staff_id);
        return aBoolean;
    }

    @Override
    public Boolean cancleclock(int staff_id) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        Boolean aBoolean = mapper.cancelClockUser(staff_id);
        return aBoolean;
    }


    @Override
    public int getTotalCount() {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        int selectcount = mapper.selectcount();
        return selectcount;
    }

    @Override
    public int getTotalPage(int totalCount, int row) {
        Integer i = null;
        if (totalCount%row ==0){
            i = totalCount/row;
        }else {
            i = (totalCount/row)+1;
        }
        return i;
    }



    @Override
    public List<UserStaff> getUserByPage(Map<String, Integer> map,int current,int row) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        map.put("startIndex",(current-1)*row);
        map.put("pageSize",row);
        List<UserStaff> userStaffs = mapper.selectByPage(map);
        return  userStaffs;
    }

    @Override
    public int findLevel(int staff_id) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        int i = mapper.selectLevelByid(staff_id);
        return i;
    }

    @Override
    public String findPwd(int staff_id) {
        //获取sqlsession对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StaffMapper mapper = sqlSession.getMapper(StaffMapper.class);
        String s = mapper.selectPwd(staff_id);
        return s;
    }

}
